/*
 * Feat.cpp
 *
 *  Created on: Jan 1, 2009
 *      Author: sez
 */

#include "Feat.h"

#include <Xml/Xml.h>
#include <Xml/XmlPath.h>

#include <QString>

const char *Feat::FEAT_TYPE_NAMES[] = {
		"Standard"
		"Class",
		"Divinity",
		"Multiclass",
		"Racial"
};

Feat::Feat(const QString &title)
	: TitledData(title)
{
}

Feat::Feat(const Feat &rhs)
	: TitledData(rhs.title())
{
	this->tier = rhs.tier;
	this->prerequisites = rhs.prerequisites;
	this->benefits = rhs.benefits;
	this->special = rhs.special;
	this->type = rhs.type;
	this->raceName = rhs.raceName;
	this->className = rhs.className;
}

Feat::~Feat()
{
}

QString Feat::description() const
{
    QString featSpecifics = QString("%1 tier ").arg(DndData::TIER_NAMES[tier]);
    switch (type)
    {
        case STANDARD_FEAT:
            // nothing specific
            break;
        case CLASS_FEAT:
            featSpecifics.append(QString("%1 %2").arg(DndData::CLASS_NAMES[className]).arg(FEAT_TYPE_NAMES[type]));
            break;
        case DIVINITY_FEAT:
        case MULTICLASS_FEAT:
            featSpecifics.append(FEAT_TYPE_NAMES[type]);
            break;
        case RACIAL_FEAT:
            featSpecifics.append(QString("%1 %2").arg(DndData::RACE_NAMES[raceName]).arg(FEAT_TYPE_NAMES[type]));
            break;
    }
    return QString("%1 feat").arg(featSpecifics);
}

void Feat::saveToXml() const
{
    Xml xml(XmlPath(XmlPath::FEAT_BASE, XmlPath::safeDataPathName(title())));
    xml.appendTag("Title", title());
    appendTag(xml, "Tier", tier);
    xml.appendTag("Prerequisites", prerequisites);
    xml.appendTag("Benefits", benefits);
    xml.appendTag("Special", special);
    appendTag(xml, "Type", type);
    appendTag(xml, "Race", raceName);
    xml.appendTag("ExtRace", extRace);
    appendTag(xml, "Class", className);
    xml.appendTag("ExtClass", extClassName);
    xml.serializeXml();
}

void Feat::loadFromXml()
{
    Xml xml(XmlPath(XmlPath::RACIAL_BASE, XmlPath::safeDataPathName(title())));
    xml.deserializeXml();
    setTitle(xml.value("Title"));
    tier = value<DndData::Tier>(xml, "Tier");
    prerequisites = xml.value("Prerequisites");
    benefits = xml.value("Benefits");
    special = xml.value("Special");
    type = value<Feat::FeatType>(xml, "Type");
    raceName = value<DndData::RaceName>(xml, "Race");
    extRace = xml.value("ExtRace");
    className = value<DndData::ClassName>(xml, "Class");
    extClassName = xml.value("ExtClass");
}

Feat &Feat::operator=(const Feat &rhs)
{
	this->tier = rhs.tier;
	this->prerequisites = rhs.prerequisites;
	this->benefits = rhs.benefits;
	this->special = rhs.special;
	this->type = rhs.type;
	this->raceName = rhs.raceName;
        this->className = rhs.className;
        return *this;
}

QTextStream & operator>>(QTextStream &s, Feat::FeatType &data)
{
    int result;
    s >> result;
    data = Feat::FeatType(result);
    return s;
}
